package com.it.base.core.system.dict.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.it.base.common.view.PageResult;
import com.it.base.common.view.Result;
import com.it.base.common.view.ResultCode;
import com.it.base.core.system.dict.model.covert.SysDictDataCovert;
import com.it.base.core.system.dict.model.dto.SysDictDataDTO;
import com.it.base.core.system.dict.model.dto.SysDictDataListDTO;
import com.it.base.core.system.dict.model.entity.SysDictData;
import com.it.base.core.system.dict.model.from.SysDictDataDeleteOneFormDTO;
import com.it.base.core.system.dict.model.from.SysDictDataSaveFormDTO;
import com.it.base.core.system.dict.model.from.SysDictDataSelectOneFormDTO;
import com.it.base.core.system.dict.model.from.SysDictDataSelectPageListFormDTO;
import com.it.base.core.system.dict.model.from.SysDictDataUpdateFormDTO;
import com.it.base.core.system.dict.model.from.SysDictDataValidatorFormDTO;
import com.it.base.core.system.dict.service.ISysDictDataService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("system/dict/data")
@ApiSort(value = 2)
@Api(tags = "2.字典数据")
public class SysDictDataController {

	@Autowired
	private ISysDictDataService sysDictDataService;

	@GetMapping("test")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "1.测试", notes = "测试")
	public Result<?> test() {
		return Result.SUCCESS();
	}

	@PostMapping("selectPageList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "2.分页查询字典数据", notes = "分页查询字典")
	public Result<PageResult<SysDictDataListDTO>> selectPageList(
			@Validated @RequestBody SysDictDataSelectPageListFormDTO sysDictDataSelectPageListFormDTO) {
		PageResult<SysDictData> page = sysDictDataService.selectPageList(sysDictDataSelectPageListFormDTO);
		List<SysDictDataListDTO> datas = SysDictDataCovert.INSTANCE.entity2DTOList(page.getRows());
		return Result.SUCCESS(new PageResult<SysDictDataListDTO>(page.getTotal(), datas));
	}

	@PostMapping("validatorExist")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "3.校验字典数据", notes = "校验字典数据")
	public Result<?> validatorExist(@Validated @RequestBody SysDictDataValidatorFormDTO sysDictDataValidatorFormDTO) {
		sysDictDataValidatorFormDTO.setId(null);
		return Result.SUCCESS(this.sysDictDataService.validatorExist(sysDictDataValidatorFormDTO));
	}

	@PostMapping("saveOne")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "4.新增字典数据", notes = "新增字典数据")
	public Result<String> saveOne(@Validated @RequestBody SysDictDataSaveFormDTO sysDictDataSaveFormDTO) {
		// 1.校验
		SysDictDataValidatorFormDTO sysDictDataValidatorFormDTO = SysDictDataValidatorFormDTO.builder()//
				.type(sysDictDataSaveFormDTO.getType())//
				.value(sysDictDataSaveFormDTO.getValue())//
				.build();
		boolean flag = this.sysDictDataService.validatorExist(sysDictDataValidatorFormDTO);
		if (flag) {
			return new Result<>(ResultCode.E_100116);
		}

		// 2.保存
		SysDictData sysDictData = SysDictDataCovert.INSTANCE.from2Entity(sysDictDataSaveFormDTO);
		this.sysDictDataService.save(sysDictData);
		return Result.SUCCESS(sysDictData.getId());
	}

	@PostMapping("selectOne")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "5.查询字典数据", notes = "查询字典数据")
	public Result<SysDictDataDTO> selectOne(
			@Validated @RequestBody SysDictDataSelectOneFormDTO sysDictDataSelectOneFormDTO) {
		SysDictData sysDictData = this.sysDictDataService.getById(sysDictDataSelectOneFormDTO.getId());
		SysDictDataDTO sysDictDataDTO = SysDictDataCovert.INSTANCE.entity2DTO(sysDictData);
		return Result.SUCCESS(sysDictDataDTO);
	}

	@PutMapping("updateOne")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "6.更新字典数据", notes = "更新字典数据")
	public Result<?> updateOne(@Validated @RequestBody SysDictDataUpdateFormDTO sysDictDataUpdateFormDTO) {
		// 1.校验
		SysDictDataValidatorFormDTO sysDictDataValidatorFormDTO = SysDictDataValidatorFormDTO.builder()//
				.type(sysDictDataUpdateFormDTO.getType())//
				.value(sysDictDataUpdateFormDTO.getValue())//
				.id(sysDictDataUpdateFormDTO.getId())//
				.build();
		boolean flag = this.sysDictDataService.validatorExist(sysDictDataValidatorFormDTO);
		if (flag) {
			return new Result<>(ResultCode.E_100116);
		}

		SysDictData sysDictData = SysDictDataCovert.INSTANCE.from2Entity(sysDictDataUpdateFormDTO);
		this.sysDictDataService.updateById(sysDictData);
		return Result.SUCCESS(sysDictData.getId());
	}

	@DeleteMapping("deleteOne")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "7.删除字典类型", notes = "删除字典")
	public Result<Object> deleteOne(@Validated @RequestBody SysDictDataDeleteOneFormDTO sysDictDataDeleteOneFormDTO) {
		this.sysDictDataService.removeById(sysDictDataDeleteOneFormDTO.getId());
		return Result.SUCCESS();
	}

}
